package com.dundun.SSO.core.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**********************************************************
 * 版权所有：搜影科技 保留所有权利
 * 创建日期: 2017/3/21 0021 15:05
 * 创建作者: gezhangkai
 * 版    本:
 * 功    能:
 * 最后修改时间:
 * 修改记录:
 ***********************************************************/
public class AuthInterceptor extends HandlerInterceptorAdapter {

    private static final Logger logger = LoggerFactory.getLogger(AuthInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler.getClass().isAssignableFrom(HandlerMethod.class)) {
            String uri = request.getRequestURI();
            logger.debug("拦截请求[{}]", uri);
            if (!permitCheck(request, response))
                return false;
        }
        return true;
    }

    private boolean permitCheck(HttpServletRequest request, HttpServletResponse response) {
//        try {
//            if (UserContext.getCurrentMemberName() == null) {
//                return true;
//            }
//            UserAccount userAccount = UserContext.getInstance().getLoginUser(request);
//            if (userAccount == null) {
//                userAccount = userAccountService.getByPassportName(UserContext.getCurrentMemberName());
//                if (userAccount == null) {
//                    com.shunwang.baseStone.sso.context.UserContext.removeUser();
//                    UserContext.getInstance().removeLoginUser(request);
//                    CookieUtil.removeCookies(DomainContext.getCookieDomain(), request, response);
//                    String errorMsg = java.net.URLEncoder.encode("结算平台账号不存在", "utf-8");
//                    response.sendRedirect("/main/login.htm?errorMsg=" + errorMsg);
//                    return false;
//                }
//                UserContext.getInstance().setLoginUser(userAccount, request);
//            }
//            if (userAccount.isClose()) { //用户被禁止登录
//                com.shunwang.baseStone.sso.context.UserContext.removeUser();
//                UserContext.getInstance().removeLoginUser(request);
//                CookieUtil.removeCookies(DomainContext.getCookieDomain(), request, response);
//                String errorMsg = java.net.URLEncoder.encode("用户被禁止登录", "utf-8");
//                response.sendRedirect("/main/login.htm?errorMsg=" + errorMsg);
//                return false;
//            } else if (userAccount.isMember()) { //普通用户
//                com.shunwang.baseStone.sso.context.UserContext.removeUser();
//                UserContext.getInstance().removeLoginUser(request);
//                CookieUtil.removeCookies(DomainContext.getCookieDomain(), request, response);
//                String errorMsg = java.net.URLEncoder.encode("个人用户不能登录本站", "utf-8");
//                response.sendRedirect("/main/login.htm?errorMsg=" + errorMsg);
//                return false;
//            }
        //  request.setAttribute("userAccount", userAccount);
//        } catch (Exception e) {
//            logger.error("权限拦截处理异常：", e);
//        }
        return true;
    }
}

